#!/bin/bash

echo "War3 Proxy 编译脚本"
echo "==================="

# 检查是否安装了Windows交叉编译工具链
if ! command -v x86_64-w64-mingw32-gcc &> /dev/null; then
    echo "警告: 未找到Windows交叉编译工具链"
    echo "安装命令: sudo apt-get install mingw-w64"
    echo ""
fi

echo "选择编译选项:"
echo "1) Linux本地编译 (包含网络接口功能)"
echo "2) Windows交叉编译 (不包含网络接口功能)"
echo "3) 全部编译"
echo "4) 清理"
echo ""

read -p "请输入选择 (1-4): " choice

case $choice in
    1)
        echo "正在编译Linux版本..."
        cargo build --release
        echo "Linux版本编译完成: target/release/war3-server, target/release/war3-client"
        ;;
    2)
        echo "正在编译Windows版本..."
        cargo build --release --target x86_64-pc-windows-gnu --no-default-features
        echo "Windows版本编译完成: target/x86_64-pc-windows-gnu/release/war3-server.exe, target/x86_64-pc-windows-gnu/release/war3-client.exe"
        ;;
    3)
        echo "正在编译Linux版本..."
        cargo build --release
        echo "Linux版本编译完成!"
        
        echo "正在编译Windows版本..."
        if command -v x86_64-w64-mingw32-gcc &> /dev/null; then
            cargo build --release --target x86_64-pc-windows-gnu --no-default-features
            echo "Windows版本编译完成!"
        else
            echo "跳过Windows编译: 缺少交叉编译工具链"
        fi
        ;;
    4)
        echo "清理编译文件..."
        cargo clean
        echo "清理完成!"
        ;;
    *)
        echo "无效选择!"
        exit 1
        ;;
esac

echo ""
echo "编译说明:"
echo "- Linux版本: 使用pnet库获取真实网络接口广播地址"
echo "- Windows版本: 使用全局广播地址255.255.255.255"
echo "- 两个版本都修复了原来的127.0.1.255广播风暴问题"